#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

import json
import os

from common.common import output_execution_result_ex
from common.common import check_path_legal
from common.logger import Logger
from generaldb.saphana.comm.saphana_const import SaphanaPath


log = Logger().get_logger("saphana.log")


class FileOperate:
    def __init__(self):
        pass
    
    @staticmethod
    def read_json_file(file_path):
        if not os.path.isfile(file_path):
            raise Exception(f"File not exist")
        try:
            with open(file_path, "r", encoding='UTF-8') as file_obj:
                json_dict = json.loads(file_obj.read())
        except Exception as exception:
            raise Exception("parse param file failed") from exception
        return json_dict

    @staticmethod
    def write_tmp_file(context, file_path):
        path_white_list = [SaphanaPath.HANA_LIVE_MOUNT_PATH, SaphanaPath.HANA_OPT_PATH]
        for single_path in path_white_list:
            if check_path_legal(file_path, single_path):
                output_execution_result_ex(file_path, context)
                return
                # 传进来的path不在/tmp/目录下不合法
        raise Exception(f"File not legal")


